<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: 04/26/2012
 * @project		: Drag & Drop
 * @description	: 
 *		- 
 *
\* ************************************************** */

class Thuchi extends MY_Controller{
    function __construct( ){
        parent::__construct( );  
		$this->load->helper( array( 'form', 'url' ) );
		$this->load->model( 'CodeGenerator_model', '', TRUE );
		$this->load->helper('date');
    }
	
	function index(){
		$this->load_template();	
		$loai = 1; //tháng
		$time=$this->input->post('thuchi_dt');		
		if(empty($time))
			$time = date("m/Y",time());
		$dt = explode("/",$time);
		if(count($dt)==2){
			$this->data['thang'] = $dt[0];
			$this->data['time'] = $time;
		}else {$this->data['thang'] = 1;
		$this->data['time'] = "";
		}
		$selected_cat=$this->input->post('theo');
		if(empty($selected_cat)) $selected_cat = 'M';
		$combobox_cats['M']="Thống kê theo tháng";
		$combobox_cats['CM']="Thống kê theo mốc";
		//$combobox_cats['Y']="Thống kê theo năm";
		$time_from=$this->input->post('thuchi_dt_from');
		$time_to=$this->input->post('thuchi_dt_to');
		if(empty($time_from))
			$time_from = date("d/m/Y",time());
		if(empty($time_to))
			$time_to = date("d/m/Y",time());
		$this->data['time_from'] = $time_from;
		$this->data['time_to'] = $time_to;
		$this->data['combobox_cats']=form_dropdown('theo', $combobox_cats,$selected_cat);
		if($selected_cat == "CM"){
			$limit['datemin']= strtotime(str_replace(array("/"),array("."),$time_from));
			$limit['datemax']= strtotime(str_replace(array("/"),array("."),$time_to));
		}else
			$limit = $this->getLimit($selected_cat,"06/".$time);
		//thong tin luong
		$sql = "select e.Personal_ID,e.Salary,e.EE_ID from employee as e where e.role=".EMPLOYER." GROUP BY e.Personal_ID";
		// die($sql);
		$results = $this->CodeGenerator_model->custom_query($sql,"array");
		foreach($results as $k =>$item){
			$ee_id = $item['EE_ID'];
			$sql = "select sum(Price_Serv) as total from tientip where EE_ID='$ee_id'";
			$total = $this->CodeGenerator_model->custom_query($sql,"array");
			$results[$k]['Price_Serv'] = 0;
			if(!empty($total)) $results[$k]['Price_Serv'] = $total[0]['total'];
			$sql = "select sum(Price_Serv) as total from phieukh where EE_ID='$ee_id'";
			$total = $this->CodeGenerator_model->custom_query($sql,"array");
			if(!empty($total)) $results[$k]['Price_Serv'] += $total[0]['total'];
			$results[$k]['thanhtien'] = $item["Salary"]+$results[$k]['Price_Serv'];
		}
		$this->data['luong'] = $results;
		
		//chi phi phat sinh
		$sql = "select * from phatsinh where Create_Dt >= ".$limit['datemin']." AND Create_Dt <= ".$limit['datemax'] ;

		$results = $this->CodeGenerator_model->custom_query($sql,"array");
		$this->data['phatsinh'] = $results;
		
		//thu
		$sql="select sum(money) as total from transaction where Create_Dt >= ".$limit['datemin']." AND Create_Dt <= ".$limit['datemax'] ;
		$total = $this->CodeGenerator_model->custom_query($sql,"array");
		$thanhtien = 0; 
		if(!empty($total)){
			$thanhtien = $total[0]['total'];
		}
		$this->data['thu'] = $thanhtien;
		
		//tien tip
		$this->data['tientip'] = $this->getTientip($limit['datemin'],$limit['datemax']);
		
		$this->template->write( 'title', 'Spa - Thu chi' );
        $this->template->write_view( 'content', 'thuchi.php',$this->data );
        $this->template->render();
		/*
		$body = '';
		ob_start ();
		$this->load->view ( 'transaction_add', $this->data );
		$body = ob_get_clean ();
		echo $body;
		exit();
		*/
    }      
}

/* End of file index.php */
/* Location: ./index.php */